package fitnesshub.controller;

import fitnesshub.pojo.dto.BuyMemberDTO;
import fitnesshub.pojo.dto.MemberLoginDTO;
import fitnesshub.pojo.dto.MemberRegisterDTO;
import fitnesshub.pojo.dto.ResetPasswordDTO;
import fitnesshub.pojo.entity.Member;
import fitnesshub.pojo.vo.MemberLoginVO;
import fitnesshub.properties.JwtProperties;
import fitnesshub.result.Result;
import fitnesshub.service.MemberService;
import fitnesshub.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 会员管理
 */
@RestController
@RequestMapping("/member")
@Slf4j
public class MemberController {

    @Autowired
    private MemberService memberService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 会员登录
     * @param memberLoginDTO 会员登录时传递过来的数据
     * @return
     */
    @PostMapping("/login")
    public Result<MemberLoginVO> login(@RequestBody MemberLoginDTO memberLoginDTO) {
        log.info("会员登录：{}", memberLoginDTO);

        Member member = memberService.login(memberLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put("memberId", member.getMemberId());
        String token = JwtUtil.createJWT(
                jwtProperties.getMemberSecretKey(),
                jwtProperties.getMemberTtl(),
                claims);

        MemberLoginVO memberLoginVO = new MemberLoginVO(
                member.getMemberId(),
                member.getUsername(),
                member.getMemberName(),
                member.getMemberType(),
                member.getAvatar(),
                token
        );

        return Result.success(memberLoginVO);
    }

    /**
     * 会员登出
     * @return
     */
    @PostMapping("/logout")
    public Result logout() {
        log.info("会员退出登录");
        return Result.success();
    }

    /**
     * 注册
     * @param memberRegisterDTO
     * @return
     */
    @PostMapping("/register")
    public Result register(@RequestBody MemberRegisterDTO memberRegisterDTO) {
        log.info("会员退注册:{}", memberRegisterDTO);
        memberService.register(memberRegisterDTO);
        return Result.success();
    }

    /**
     * 获取会员信息
     * @return
     */
    @GetMapping("/info/{memberId}")
    public Result<Member> getMemberInfo(@PathVariable Integer memberId) {
        log.info("获取会员信息");
        Member member = memberService.getMemberInfo(memberId);
        return Result.success(member);
    }

    /**
     * 购买会员
     * @param buyMemberDTO
     * @return
     */
    @PutMapping("/update/buyMember")
    public Result buyMember(@RequestBody BuyMemberDTO buyMemberDTO) {
        log.info("购买会员:{}", buyMemberDTO);
        memberService.buyMember(buyMemberDTO);
        return Result.success();
    }

    /**
     * 更新用户信息
     * @param member
     * @return
     */
    @PutMapping("/update")
    public Result updateMemberInfo(@RequestBody Member member) {
        log.info("更新用户信息:{}", member);
        memberService.updateMemberInfo(member);
        return Result.success();
    }

    /**
     * 用户重置密码
     * @param resetPasswordDTO
     * @return
     */
    @PostMapping("/restPassword")
    public Result resetPassword(@RequestBody ResetPasswordDTO resetPasswordDTO) {
        log.info("用户重置密码:{}", resetPasswordDTO);
        memberService.resetPassword(resetPasswordDTO);
        return Result.success();
    }
}
